package com.ssm.spring;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class GoodsTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("classpath:spring-root.xml");
        System.out.println(applicationContext.getBean(GoodsService.class));


        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(GoodsTest.class);
        AnnotatedBeanDefinitionReader beanDefinitionReader = new AnnotatedBeanDefinitionReader(context);
        beanDefinitionReader.register(GoodsService.class);
        System.out.println(context.getBean(GoodsService.class));

        AnnotationConfigApplicationContext context1 = new AnnotationConfigApplicationContext(GoodsTest.class);
        ClassPathBeanDefinitionScanner scannedGenericBeanDefinition = new ClassPathBeanDefinitionScanner(context1);
        scannedGenericBeanDefinition.scan("io.zhengsh");
        System.out.println(context1.getBean(GoodsService.class));


        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        AbstractBeanDefinition beanDefinition1 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
        beanDefinition1.setBeanClass(GoodsService.class);
        beanFactory.registerBeanDefinition("goodsService", beanDefinition1);
        System.out.println(beanFactory.getBean("goodsService"));
    }
}
